package com.lzx.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.time.Duration;

/**
 * @Author: lzx
 * @Date: 2021/3/10 15:48
 */
@Configuration
public class EsConfig {
    @Value("${es.host-port}")
    private String hostAndPort;
    @Value("${es.connect-timeout}")
    private Integer connectTimeout;
    @Value("${es.socket-timeout}")
    private Integer socketTimeout;

    @Bean
    RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration configuration = ClientConfiguration.builder()
                .connectedTo(hostAndPort)
                .withConnectTimeout(Duration.ofSeconds(connectTimeout))
                .withSocketTimeout(Duration.ofSeconds(socketTimeout))
                .build();
        return RestClients.create(configuration).rest();
    }
}
